package com.nxmu.hire.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nxmu.hire.common.exception.ServiceException;
import com.nxmu.hire.dao.UserMapper;
import com.nxmu.hire.model.User;
import com.nxmu.hire.model.UserExample;
import com.nxmu.hire.model.EnumMap.EnumMap;
import com.nxmu.hire.model.enums.RoleEnum;
import com.nxmu.hire.model.param.CollegeParam;
import com.nxmu.hire.model.param.ParameterFactory;
import com.nxmu.hire.service.TeacherService;
import com.nxmu.hire.vo.FindTeacherReqVo;
import com.nxmu.hire.vo.FindTeacherResVo;
import com.nxmu.hire.vo.TeacherAddReqVo;

/**
 * @author Lee
 */
@Service
public class TeacherServiceImpl implements TeacherService {

	private final static Logger LOGGER = LoggerFactory.getLogger(TeacherServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

	@Autowired
	ParameterFactory parameterFactory;

	/*
	  添加教师
	 */
	@Transactional
	@Override
	public void saveTeacher(TeacherAddReqVo teacherAddReqVo) throws ServiceException {
		User user = new User();
		BeanUtils.copyProperties(teacherAddReqVo,user);
		user.setCollegeName(EnumMap.collegeEnumMap.get(user.getCollegeId()));
		user.setResearchName(CollegeParam.getResearch(parameterFactory.collegeEnumCollegeParamMap,user.getResearchId()).getResearchName());

		user.setPassword(teacherAddReqVo.getUserId());
		user.setRole(RoleEnum.TEACHER);
		user.setCreateTime(new Date());
		user.setLastUpdateTime(new Date());
		try {
			userMapper.insert(user);
		}catch (Exception e) {
			LOGGER.error("用户保存失败");
			throw new ServiceException("用户保存失败,原因可能是编号已经被使用！");
		}
	}

	/**
	 * 分页查询教师信息
	 *
	 * @param findTeacherReqVo
	 * @return
	 */
	@Override
	public PageInfo<User> findTeacherByPage(FindTeacherReqVo findTeacherReqVo) {
		PageHelper.startPage(findTeacherReqVo.getPageNum(), findTeacherReqVo.getPageSize());
		UserExample userExample = new UserExample();
		UserExample.Criteria userExampleCriteria = userExample.createCriteria();
		if (!StringUtils.isEmpty(findTeacherReqVo.getUserName())) userExampleCriteria.andUserNameLike("%" + findTeacherReqVo.getUserName() + "%");
		if (!StringUtils.isEmpty(findTeacherReqVo.getCollegeId())) userExampleCriteria.andCollegeIdEqualTo(findTeacherReqVo.getCollegeId());
		List<User> userList = userMapper.selectByExample(userExample);
		userList.forEach(user -> user.setPassword(null));
		return new PageInfo<>(userList);
	}

	/**
	 * 按照编号查询教师信息
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public FindTeacherResVo findTeacherByUserId(String userId) {
		FindTeacherResVo findTeacherResVo = new FindTeacherResVo();
		User user = userMapper.selectByPrimaryKey(userId);

		if (user == null) return null;

		BeanUtils.copyProperties(user,findTeacherResVo);
		findTeacherResVo.setCollegeName(EnumMap.collegeEnumMap.get(user.getCollegeId()));
		return findTeacherResVo;
	}

	/**
	 * 根据id删除教师信息
	 * @param userId
	 */
	@Override
	public void deleteById(String userId) throws ServiceException{
		try {
			userMapper.deleteByPrimaryKey(userId);
		} catch (Exception e) {
			LOGGER.error("教师信息删除失败");
			throw new ServiceException("教师信息删除失败");
		}
	}

	/**
	 * 修改教师信息
	 * @param user
	 * @throws ServiceException
	 */
	@Override
	public void updateTeacher(User user) throws ServiceException {
		try {
			userMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			LOGGER.error("教师信息修改失败");
			throw new ServiceException("教师信息修改失败");
		}
	}
}
